﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AmorCanino.Web.Models.Services;
using System.IO;
using AmorCanino.Web.Models.Models;
using AmorCanino.Web.Models.Authentication;

namespace AmorCanino.Web.Controllers
{
    public class ServicesController : BaseController
    {

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Adoptar()
        {
            var adoptionService = new AdoptionService();
            var adoptModel = new Models.Models.AdoptPet();
            var pets = adoptionService.getPagedPets(1);
            CheckForPicture(pets);

            adoptModel.Pets = pets;
            adoptModel.TotalPages = adoptionService.getTotalPages();

            return View(adoptModel);
        }

        [HttpPost]
        public JsonResult GetAdoptedByPage(int page)
        {
            var pets = new AdoptionService().getPagedPets(page);
            CheckForPicture(pets);
            return new JsonResult() { Data = pets };
        }

        public ActionResult Cruzas()
        {
            return View();
        }

        public ActionResult Search()
        {
            return View();
        }

        public ActionResult Detalle(int itemId)
        {
            var pet = new PetService().getPetById(itemId);
            CheckForPicture(pet);
            return View(pet);
        }

        [HttpPost, Authorize]
        public JsonResult SetMessage(OuterMessage message)
        {
            var currentUserId = AuthUser.Current().id;

            new MessagingService().SetMessages(new Communication()
            {
                UserId = currentUserId.ToString(),
                PublishedDate = DateTime.UtcNow,
                Message = string.Format("{0}\n{1}", (message.ContactType == ContactTypeEnum.Adopt ? 
                    string.Format(Resources.LanguageResource.AdoptPetChatMessage, message.PetName) :
                    string.Format(Resources.LanguageResource.BirthPetChatMessage, message.PetName)),
                    message.AdditionalMessage)
            }, currentUserId, message.DestinationUserID);
            return new JsonResult() { Data = new { status = "ok" } };
        }
    }
}